package cz.gillan.housecosts;

import java.util.Date;

import android.app.ListActivity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.ListView;
import cz.gillan.housecosts.data.StateImpl;
import cz.gillan.housecosts.sql.StateDbAdapter;

public class StatesActivity extends ListActivity {
	private final static int ACTIVITY_UPDATE_STATE = 0;
	private final static int CM_UPDATE_STATE = 0;
	private final static int CM_DELETE_STATE = 1;
	private StateDbAdapter stateDbAdapter;
	private Long gaugeId;
	private ListView list;
	private Cursor cursor;
	
	@Override
	/** Called when the activity is first created. */
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.states_list);
		list = getListView();
		list.setDividerHeight(2);
		registerForContextMenu(list);
		stateDbAdapter = new StateDbAdapter(this);
		stateDbAdapter.open();
		
		Bundle extras = getIntent().getExtras();
		gaugeId = (savedInstanceState == null) ? null : (Long)savedInstanceState.getSerializable(StateDbAdapter.KEY_GAUGEID);
		if (extras != null) {
			gaugeId = extras.getLong(StateDbAdapter.KEY_GAUGEID);
		}
		
		fillData();
	}
	
	@Override
	public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
		super.onCreateContextMenu(menu, v, menuInfo);
		AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo)menuInfo;
		StateImpl state = getStateFromCursor(cursor, info.position);
	    menu.setHeaderTitle(state.getFormatedDate());
	    String[] menuItems = getResources().getStringArray(R.array.statesListContextMenu);
	    for (int i = 0; i < menuItems.length; i++) {
	    	menu.add(Menu.NONE, i, i, menuItems[i]);
	    }
	}
	
	@Override
	public boolean onContextItemSelected(MenuItem item) {
		AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
		StateImpl state = getStateFromCursor(cursor, info.position);
		switch (item.getItemId()) {
		case CM_UPDATE_STATE: 
			updateState(state);
			return true;
		case CM_DELETE_STATE:
			stateDbAdapter.deleteState(state);
			fillData();
			return true;
		}
		return super.onContextItemSelected(item);
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
		super.onActivityResult(requestCode, resultCode, intent);
		fillData();
	}
	
	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		super.onListItemClick(l, v, position, id);
		
		StateImpl state = getStateFromCursor(cursor, position);
		updateState(state);
	}
	
	private StateImpl getStateFromCursor(Cursor cursor, int position) {
		StateImpl state = new StateImpl() ;
		cursor.moveToPosition(position);
		state.setId(cursor.getLong(cursor.getColumnIndexOrThrow(StateDbAdapter.KEY_ROWID)));
		state.setGaugeId(cursor.getLong(cursor.getColumnIndexOrThrow(StateDbAdapter.KEY_GAUGEID)));
		state.setState(cursor.getDouble(cursor.getColumnIndexOrThrow(StateDbAdapter.KEY_STATE)));
		state.setDate(new Date(cursor.getLong(cursor.getColumnIndexOrThrow(StateDbAdapter.KEY_DATE)) * 1000l));
		state.setComment(cursor.getString(cursor.getColumnIndexOrThrow(StateDbAdapter.KEY_COMMENT)));
		
		
		return state;
	}
	
	private void updateState(StateImpl state ) {
		Intent i = new Intent(this, StateEditActivity.class);
		i.putExtra(StateDbAdapter.KEY_GAUGEID, state.getGaugeId());
		i.putExtra(StateDbAdapter.KEY_ROWID, state.getId());
		startActivityForResult(i, ACTIVITY_UPDATE_STATE);
	}
	
	private void fillData() {
		cursor = stateDbAdapter.fetchAllStates(gaugeId);
		startManagingCursor(cursor);
		StateAdapter adapter = new StateAdapter(this, cursor);
		setListAdapter(adapter);
	}
}
